﻿using _2308NA.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _2308NA.DAL
{
    public class MenusDal
    {
        SQLHelp help = new SQLHelp();
        public List<Menus> GetMenus() {
            string zz ="yy";
            int y = 1;
            
            List<Menus> list = new List<Menus>();   
            string sql = "select * from Menus";
            SqlDataReader sdr = help.ExecuteReader(sql,null);
            if (sdr.HasRows)
            {
                while (sdr.Read())
                {
                    Menus menu = new Menus();
                    menu.MenuID = (int)sdr["menuID"];
                    menu.MenuName = sdr["menuName"].ToString();
                    menu.URL = sdr["url"].ToString();
                    menu.ParentID = (int)sdr["parentID"];

                    list.Add(menu);
                }
            }
            sdr.Close();

            return list;
        }

        public List<Menus> GetMenusByUserID(int uid)
        {

            List<Menus> list = new List<Menus>();
            string sql = @"select m.* from Menus m 
                        inner join Authority a on m.menuID=a.menuID 
                        where roleID =(select roleID from Users where userID=@uid)";
            SqlParameter[] parameters = new SqlParameter[] { 
                new SqlParameter("@uid",uid)
            };
            SqlDataReader sdr = help.ExecuteReader(sql, parameters);
            if (sdr.HasRows)
            {
                while (sdr.Read())
                {
                    Menus menu = new Menus();
                    menu.MenuID = (int)sdr["menuID"];
                    menu.MenuName = sdr["menuName"].ToString();
                    menu.URL = sdr["url"].ToString();
                    menu.ParentID = (int)sdr["parentID"];

                    list.Add(menu);
                }
            }
            sdr.Close();

            return list;
        }
    }
}
